﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eaf.Net.AccessControl.BusinessManager.PagePermission;
using Eaf.Net.AccessControl.Entity.User;
using Eaf.Net.AccessControl.DataAccess.Permission;
using System.Web;
using Eaf.Net.AccessControl.DataAccess.Role;
using System.Web.SessionState;

namespace Hfm.Core.BusinessManager.PagePermission
{
    public class AMPagePermissionManager : PagePermissionManager<UserAccountEntity>
    {
        protected readonly RoleData<UserAccountEntity> RoleDataAccess;

        public AMPagePermissionManager(PermissionData<UserAccountEntity> dataAccess, RoleData<UserAccountEntity> roDataAccess)
            : base(dataAccess)
        {
            RoleDataAccess = roDataAccess;
            this.LoginURL = "~/User/Login.aspx";
            this.NoPermissionURL = "~/NoPermission.aspx";
        }

        public bool CheckPerission(int[] allowVisitRoleIds)
        {
            CheckLogin();

            bool havePermission = false;

            HttpResponse response = HttpContext.Current.Response;

            foreach (int roleId in allowVisitRoleIds)
            {
                if (RoleDataAccess.IsUserInRole(GetLoginedUserId(), roleId))
                {
                    havePermission = true;
                    break;
                }
            }

            if (!havePermission)
            {
                response.Redirect(NoPermissionURL);
            }

            return havePermission;
        }

        public override bool CheckPerission(int allowVisitRoleId)
        {
            return CheckPerission(new int[] { allowVisitRoleId });
        }
    }
}
